package com.hu.util;

import com.hu.common.Constants;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.shaded.jackson2.org.yaml.snakeyaml.DumperOptions;
import org.apache.flink.shaded.jackson2.org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: hujianjun
 * @Create Date: 2021/1/29 14:43
 * @Describe:
 */
public class YamlUtil {

    public static Map<?, ?> loadYaml(String... fileNames) throws FileNotFoundException {
        Map<?, ?> map = new HashMap<>();

        if (fileNames != null) {
            for (String file : fileNames) {
                InputStream in = YamlUtil.class.getClassLoader().getResourceAsStream(file);
                if (Objects.nonNull(map)) {
                    map.putAll(new Yaml().load(in));
                    continue;
                }
                map = (LinkedHashMap<?, ?>) new Yaml().load(in);
            }
        }
        return map;
    }

    public static Map<?, ?> loadYamlByFilePath(String... filePath) throws FileNotFoundException {
        Map<?, ?> map = null;
        if (filePath != null) {
            for (String fp : filePath) {
                File file = new File(fp);
                FileInputStream fileInputStream = new FileInputStream(file);
                if (map != null) {
                    map.putAll(new Yaml().load(fileInputStream));
                    continue;
                }
                map = (LinkedHashMap<?, ?>) new Yaml().load(fileInputStream);
            }
        }
        return map;
    }

    public static void dumpYaml(String fileName, Map<?, ?> map) throws IOException {
        if (StringUtils.isNotEmpty(fileName)) {
            FileWriter fileWriter = new FileWriter(YamlUtil.class.getResource(fileName).getFile());
            DumperOptions options = new DumperOptions();
            options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
            Yaml yaml = new Yaml(options);
            yaml.dump(map, fileWriter);
        }
    }

    public static Object getProperty(Map<?, ?> map, Object qualifiedKey) {
        if (map != null && !map.isEmpty() && qualifiedKey != null) {
            String input = String.valueOf(qualifiedKey);
            if (!input.equals(Constants.EMPTY_STRING)) {
                if (input.contains(Constants.POINT_SYMBOL)) {
                    int index = input.indexOf(Constants.POINT_SYMBOL);
                    String left = input.substring(0, index);
                    String right = input.substring(index + 1, input.length());
                    return getProperty((Map<?, ?>) map.get(left), right);
                } else if (map.containsKey(input)) {
                    return map.get(input);
                } else {
                    return null;
                }
            }
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public static void setProperty(Map<?, ?> map, Object qualifiedKey, Object value) {
        if (map != null && !map.isEmpty() && qualifiedKey != null) {
            String input = String.valueOf(qualifiedKey);
            if (!input.equals(Constants.EMPTY_STRING)) {
                if (input.contains(Constants.POINT_SYMBOL)) {
                    int index = input.indexOf(Constants.POINT_SYMBOL);
                    String left = input.substring(0, index);
                    String right = input.substring(index + 1, input.length());
                    setProperty((Map<?, ?>) map.get(left), right, value);
                } else {
                    ((Map<Object, Object>) map).put(qualifiedKey, value);
                }
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        Map<?, ?> map = YamlUtil.loadYaml("application.yml");
        System.out.println(YamlUtil.getProperty(map, "dataSource.mysql.driver"));
    }
}

